package project.dade.cn.bst.adapter.base;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.LinkedList;

import project.dade.cn.bst.R;
import project.dade.cn.bst.activity.CommentActivity;
import project.dade.cn.bst.activity.OrderDetailActivity;
import project.dade.cn.bst.activity.RefundActivity;
import project.dade.cn.bst.model.MyordrerModel;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.ULog;

/**
 * Created by vain zhou on 2016/4/15.
 */
public class OrderAdapter extends BaseAdapter {
    private LinkedList<MyordrerModel.OrderProductsBean> OrderProducts;
    private Context mContext;
    private ViewHolder viewHolder = null;

    public OrderAdapter(Context context, LinkedList<MyordrerModel.OrderProductsBean> OrderProducts) {
        this.mContext = context;
        this.OrderProducts = OrderProducts;
    }

    @Override
    public int getCount() {
        if(OrderProducts != null && ! OrderProducts.isEmpty()){
            return OrderProducts.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return OrderProducts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View viewRoot = convertView;
        if (viewRoot == null) {
            viewRoot = LayoutInflater.from(mContext).inflate(R.layout.adapter_order, null);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) viewRoot.findViewById(R.id.tv_title);
            viewHolder.sell = (TextView) viewRoot.findViewById(R.id.tv_sell);
            viewHolder.number=(TextView) viewRoot.findViewById(R.id.tv_num) ;
            viewHolder.photo=(ImageView) viewRoot.findViewById(R.id.photo);
            viewHolder.bar_commtent=(LinearLayout)viewRoot.findViewById(R.id.line_comment);
            viewHolder.comment=(Button)  viewRoot.findViewById(R.id.btn_order_comment);
            viewHolder.bar_refund=(LinearLayout) viewRoot.findViewById(R.id.label_refund) ;
            viewHolder.refund=(TextView) viewRoot.findViewById(R.id.refund);
            viewHolder.paid=(TextView) viewRoot.findViewById(R.id.tv_paid);
            viewRoot.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) viewRoot.getTag();
        }

        viewHolder.name.setText(OrderProducts.get(position).getProductName());
        viewHolder.sell.setText(OrderProducts.get(position).getTotalPrice()+"");
        viewHolder.number.setText(OrderProducts.get(position).getQuantity()+"");

        viewHolder.paid.setText(OrderProducts.get(position).getFactPrice()+"");
        Glide.with(mContext).load(OrderProducts.get(position).getProductImage()).placeholder(R.drawable.image_loader_small).error(R.drawable.image_loader_small).into(viewHolder.photo);
        if(OrderProducts.get(position).getIsSigned()==1/*已签收*/)
        {
         viewHolder.comment.setVisibility(View.VISIBLE);
            if(OrderProducts.get(position).getCommentType()==0/*未评论*/)
            {
                //显示评论按钮
                viewHolder.bar_commtent.setVisibility(View.VISIBLE);
                viewHolder.comment.setText("评论商品");

            }

        }
        else
        {
            if (OrderProducts.get(position).getPaymentStatus() == 1) {

                if (OrderProducts.get(position).getIsRefund().equals("0"))/*未申请退款*/ {

                        viewHolder.bar_commtent.setVisibility(View.VISIBLE);
                        viewHolder.comment.setText("申请退款");


                }
                if (OrderProducts.get(position).getIsRefund().equals("1"))/*该商品已申请退款*/ {
                    //显示退款状态

                    String refundState=OrderProducts.get(position).getRefundState().trim();
                   // boolean r=refundState.equals("0");
                    //boolean r2=refundState.equals(0);
                    //ULog.d("RefundState", "数据 " +OrderProducts.get(position).getOrderNo()+"--------"+refundState+"--------boolean r1"+r+"--------boolean r2"+r2);
                    if (refundState.equals("0"))/*退款等待审核*/ {

                        //等待退款审核
                        viewHolder.bar_commtent.setVisibility(View.GONE);
                        viewHolder.bar_refund.setVisibility(View.VISIBLE);
                        viewHolder.refund.setText("退款待审核");
                    }
                    if (refundState.equals("1"))/*等待退款*/ {
                        //等待退款
                        ULog.d("等待退款", "----------------------------------------------------");
                        viewHolder.bar_commtent.setVisibility(View.GONE);
                        viewHolder.bar_refund.setVisibility(View.VISIBLE);
                        viewHolder.refund.setText("等待卖家退款");
                    }
                    if (refundState.equals("2") )/*等待买家退货*/{
                        //等待买家退货
                        viewHolder.bar_commtent.setVisibility(View.GONE);
                        viewHolder.bar_refund.setVisibility(View.VISIBLE);
                        viewHolder.refund.setText("等待买家退货");
                    }
                    if (refundState.equals("3"))/*已退款*/ {
                        //已退款
                        viewHolder.bar_commtent.setVisibility(View.GONE);
                        viewHolder.bar_refund.setVisibility(View.VISIBLE);
                        viewHolder.refund.setText("卖家已退款");
                    }
                    if (refundState.equals("4"))/*卖家拒绝退款*/ {
                        //卖家拒绝退款

                        viewHolder.bar_commtent.setVisibility(View.GONE);
                        viewHolder.bar_refund.setVisibility(View.VISIBLE);
                        viewHolder.refund.setText("卖家拒绝退款");
                    }
                }
            }
        }

        viewHolder.comment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(OrderProducts.get(position).getIsSigned()==1&&OrderProducts.get(position).getCommentType()==0){
                    Intent mIntent = new Intent(mContext,
                           CommentActivity.class);
                    mIntent.putExtra("orderno",OrderProducts.get(position).getOrderNo());
                    mIntent.putExtra("orderimage",OrderProducts.get(position).getProductImage());
                    mIntent.putExtra("price",OrderProducts.get(position).getFactPrice());
                    mIntent.putExtra("title",OrderProducts.get(position).getProductName());
                    mIntent.putExtra("id",OrderProducts.get(position).getProductId()+"");
                 mContext. startActivity(mIntent);
                }else {
                    Intent mIntent = new Intent(mContext,
                            RefundActivity.class);
                    mIntent.putExtra("orderno",OrderProducts.get(position).getOrderNo());
                    mIntent.putExtra("orderimage",OrderProducts.get(position).getProductImage());
                    mIntent.putExtra("price",OrderProducts.get(position).getPrice()+"");
                    mIntent.putExtra("title",OrderProducts.get(position).getProductName());
                    mIntent.putExtra("id",OrderProducts.get(position).getProductId()+"");
                    mIntent.putExtra("num",OrderProducts.get(position).getQuantity()+"");
                    mContext. startActivity(mIntent);

                }
            }
        });

        viewRoot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent mIntent = new Intent(mContext,
                        OrderDetailActivity.class);
                mIntent.putExtra("orderno",OrderProducts.get(position).getOrderNo());
                mContext. startActivity(mIntent);
            }
        });


        return viewRoot;
    }
    class ViewHolder{
        TextView name;
        TextView sell;
        TextView number;
        ImageView photo;
        LinearLayout bar_commtent;
        Button comment;
        LinearLayout bar_refund;
        TextView refund;
        TextView paid;
    }
}
